<?php
/**
 * @authors	sachin,shine,jonia
 * @copyright   CMM Technologies
 * @date        14-11-2011
 * @file	Project.cls.php
 * @description manipulation of common functions are handled here.
 */

 class Project extends Base
 {
	 
	/**
     * Project id
     * @var integer
     */
	 private $id;

	/**
     * Project name
     * @var string
     */
	 private $projectName;

	

	/**
     * Description 
     * @var string
     */
	 private $description;

	/**
     *Client id
     * @var integer
     */
	 private $clientsId;

	/**
     * starting date
     * @var date
     */
	 private $startingDate;

	/**
     * Ending date
     * @var date
     */
	  private $deadline;

	 /**
     * Modified date
     * @var date
     */
	 private $endingDate;

	 /**
     * Modified date
     * @var date
     */
	 private $modifiedDate;

	

	 public function __construct($id)
	 {
		parent::init();

		$this->id=$id;

		$data=self::$db->getDBContentsBySql("SELECT * from tms_projects WHERE id='$id'");
		$this->projectName=$data[0]['project_name'];
		$this->description=$data[0]['project_description'];
		$this->clientsId=$data[0]['clients_id'];
		$this->startingDate=$data[0]['starting_date'];
		$this->deadline=$data[0]['deadline'];
		$this->endingDate=$data[0]['ending_date'];
		$this->modifiedDate=$data[0]['modified_date'];
		
	 }
	
	/**
     * Function to get the project name
	 *return string project name 
	 */
	 public function getProjectName()
	 {
		 return $this->projectName;
	 }
		
	
	/**
     * Function to get the  description
	 *return string  description 
	 */
	  public function getDescription()
	 {
		 return $this->description;
	 }
	

	/**
     * Function to get the client id
	 *return integer client id  
	 */
	  public function getClientId()
	 {
		 return $this->clientsId;
	 }

	 
	/**
     * Function to get the starting date
	 *return date starting date
	 */

	  public function getStartingDate()
	 {
		 return $this->startingDate;
	 }
	  

/**
     * Function to get the deadline date
	 *return date deadline date
	 */

	  public function getDeadline()
	 {
		 return $this->deadline;
	 }
	  

	   
	/**
     * Function to get the ending date
	 *return date ending date
	 */

	  public function getEndingDate()
	 {
		 return $this->endingDate;
	 }
	 
	/**
     * Function to get the modified date
	 *return date  modified date
	 */
	  public function getModifiedDate()
	 {
		 return $this->modifiedDate;
	 }


	/**
     * Function to return list of projects
	 *return array  list of projects
	 * @param string $order - optional parameter for specifiying order of results
	 * @param filter $filter - optional parameter for filtering results 
	 */

	 public static function getProjects($order="project_name",$filter="")
	 {
		 parent::init();
		 
		 $newfilter['project_name']=$filter;
		 $newfilter['project_description']=$filter;

		$arr = self::$db->getDBFieldByCond("tms_projects","id","",$order,$newfilter);
		return $arr;

	 }

	 /**
     * Function to return list of modules in the project
	 *return array  list of modules
	 */

	 public function getModules($order="module_name",$filter="1")
	 {

		 $arr = self::$db->getDBFieldByCond("tms_project_modules","id","projects_id='$this->id'",$order,$filter);
		 return $arr;

	 }

	 /**
     * Function to return list of members in the project
	 *return array  list of members
	 */

	 public function getMembers()
	 {

		 $arr = self::$db->getDBFieldByCond("tms_project_members","users_id","projects_id='$this->id'");
		 return $arr;

	 }


	 /**
     * Function to return list of tasks in the project
	 *return array  list of tasks
	 */

	 public function getTasks()
	 {
		 $cond = "project_modules_id= ANY (SELECT id FROM tms_project_modules WHERE projects_id='$this->id')";
		 $arr = self::$db->getDBFieldByCond("tms_task","id",$cond);
		 return $arr;

	 }

	 /**
     * Function to return list of tasks in the project
	 * belonging to a particular module
	 *return array  list of tasks
	 */

	 public  function getTasksByModule($moduleID)
	 {

		 $cond = "project_modules_id=$moduleID";
		 $arr = self::$db->getDBFieldByCond("tms_task","id",$cond);
		 return $arr;

	 }

	 /**
	 * addModule
	 *
     * A function to add a module
	 * into a project 
	 *@param string $moduleName
	 *@return boolean - true if successful or false on failure
	 */

	 public function addModule($moduleName)
	 {

		
		 $fields = "projects_id,module_name";
		 $valuesArr = array("$this->id","$moduleName");
		 
		 $status = self::$db->dbInsert("tms_project_modules",$fields,$valuesArr);

		 if($status==0||$status=="Error Occured")
			 return false;
		 ELSE 
			 return true;


	 }

	 /**
	 * addMember
	 *
     * A function to add a member
	 * into a project 
	 *@param int $userID
	 *@param string $memberRole
	 *@return boolean - true if successful or false on failure
	 */

	 public function addMember($userID,$memberRole)
	 {
		 $fields = "users_id,projects_id,member_role";
		 $valuesArr = array("$userID","$this->id","$memberRole");

		 $status = self::$db->dbInsert("tms_project_members",$fields,$valuesArr);
		
		 if($status==0||$status=="Error Occured")
			 return false;
		 else 
			 return true;

	 }


	 /**
	 * addProject
	 *
     * A function to add a project
	 *@param int $userID
	 *@param string $memberRole
	 *@return boolean - true if successful or false on failure
	 */

	 public static function addProject($name,$description,$clientID,$deadline)
	 {
		 parent::init();

		 $fields = "project_name,project_description,clients_id,deadline";
		 $valuesArr = array("$name","$description","$clientID","$deadline");
		 $status = self::$db->dbInsert("tms_projects",$fields,$valuesArr);

		 if($status==0||$status=="Error Occured")
			 return false;
		 else 
			 return true;

	 }

	 /**
	 * getProjectNameById
	 *
     * A function to get the name of a project
	 * given the id
	 *@param int $id
	 *@return string - the name of project
	 */

	 public static function getProjectNameById($id)
	 {
		 parent::init();

		$result=self::$db->getCellContentByCondition("tms_projects","project_name","id=$id");
		return $result;

	 }

	  public static function deleteProject($id)
	 {
		 parent::init();
		$qry="DELETE FROM tms_projects WHERE id='$id'";
		$result=self::$db->executeQuery($qry);
		return $result;

	 }

	 	public  function updateProject($editProjectName,$editProjectDesc,$editProjectClient,$editProjectDeadline)
	 {
		
		$id=$this->id;
		$field= "project_name='$editProjectName',project_description='$editProjectDesc',clients_id='$editProjectClient',deadline='$editProjectDeadline'   WHERE `id` = '$id'";
		$edit= self::$db->dbUpdate("tms_projects",$field);
		return $edit;

	 }
 


 }
 ?>